﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    /// <remarks>
    /// Unfortunately, I repeat some of the code from AddNewEmployee form.
    /// I wrote the other one
    /// </remarks>
    public partial class UpdateEmployee : Form
    {
        internal DataRow Employee;

        public UpdateEmployee()
        {            
            InitializeComponent();
        }

        void AbsorbRowData(DataRow emp)
        {
            AbsorbRowData(
                emp,
                tbFirstName,
                tbLastName,
                rbMale,
                rbFemale,
                tbPhoneNumber,
                tbMobilePhone,
                tbAddress,
                tbNIN,
                dpBirthDate);            
        }

        public UpdateEmployee(DataRow row, InputAction action)
            : this()
        {
            if (action == InputAction.Update)
            {
                Employee = row;
                AbsorbRowData(Employee);
            }
            else
            {
                btnClear_Click(null, null);
            }
        }

        #region static
        // TODO: wypelnij komentarze
        /// <summary>
        /// 
        /// </summary>
        /// <param Name="Name">TextBox containing first Name</param>
        /// <param Name="surname">TextBox containing last Name</param>
        /// <param Name="male">RadioButton determining whether _employee is male</param>
        /// <param Name="female">RadioButton determining whether _employee is female</param>
        /// <param Name="phone">MaskedTextBox containing phone number</param>
        /// <param Name="mobile">MaskedTextBox containing mobile phone number</param>
        /// <param Name="address">TextBox containing _employee'pShift address</param>
        /// <param Name="NIN">MaskedTextBox containing _employee'pShift NIN</param>
        /// <param Name="DOB">DateTimePicker containing _employee'pShift date of birth</param>
        /// <returns>
        /// Key = 1 (no problem), 0 (warning; ask for confirmation), -1 (error)
        /// Value = the pMessage to display (if there was no problem, Empty string).
        /// </returns>
        /// <remarks>
        /// </remarks>
        public static KeyValuePair<FormValidationResult, string> DoesValidate(TextBox name, TextBox surname, RadioButton male, RadioButton female, MaskedTextBox phone, MaskedTextBox mobile, TextBox address, MaskedTextBox NIN, DateTimePicker DOB)
        {
            var validated = FormValidationResult.Fine;
            var message = String.Empty;
            #region absolutely necessary
            if (name.Text.Trim() == String.Empty)
            {
                message += "*\tSupply the first name of the employee\n";
                validated = FormValidationResult.Error;
            }
            if (surname.Text.Trim() == String.Empty)
            {
                message += "*\tSupply the last name of the employee\n";
                validated = FormValidationResult.Error;
            }
            if (!(male.Checked || female.Checked))
            {
                message += "*\tSpecify the gender of the employee\n";
                validated = FormValidationResult.Error;
            }
            #endregion
            if (validated == FormValidationResult.Error)
            {
                return new KeyValuePair<FormValidationResult, string>(
                    validated,
                    message);
            }
            #region missing info be supplied later, but user will be warned. also weird info (such as _employee 8 yrs old) causes a warning
            if ((DOB.Value.AddYears(14) > DateTime.Now) || (DOB.Value.AddYears(80) < DateTime.Now))
            {
                message += "*\tThe birth date is nonsensical\n";
                validated = FormValidationResult.Problem;
            }
            if (phone.Text.Trim() == String.Empty)
            {
                message += "*\tNo phone number supplied\n";
                validated = FormValidationResult.Problem;
            }
            if (mobile.Text.Trim() == String.Empty)
            {
                message += "*\tNo mobile phone number supplied\n";
                validated = FormValidationResult.Problem;
            }
            if (address.Text.Trim() == String.Empty)
            {
                message += "*\tNo address supplied\n";
                validated = FormValidationResult.Problem;
            }
            if (NIN.Text.Trim() == String.Empty)
            {
                message += "*\tNo National Insurance Number supplied\n";
                validated = FormValidationResult.Problem;
            }
            if (DOB.Value.AddYears(18) > DateTime.Now ||
                DOB.Value.AddYears(64) < DateTime.Now)
            {
                message += "*\tAre you sure the birth date is correct\n";
                validated = FormValidationResult.Problem;
            }            
            #endregion

            return new KeyValuePair<FormValidationResult, string>(validated, message);
        }

        public static void AbsorbRowData(DataRow emp, TextBox name, TextBox surname, RadioButton male, RadioButton female, MaskedTextBox phone, MaskedTextBox mobile, TextBox address, MaskedTextBox NIN, DateTimePicker DOB)
        {
            name.Text = emp[1].ToString();
            surname.Text = emp[2].ToString();
            phone.Text = emp[3].ToString();
            mobile.Text = emp[4].ToString();
            try
            {
                DOB.Value = DateTime.Parse(emp[5].ToString()).Date;
            }
            catch { }
            male.Checked = (bool)emp[6];
            female.Checked = !male.Checked;
            NIN.Text = emp[8].ToString();
        }

        public static void FillRow(DataRow emp, TextBox name, TextBox surname, RadioButton male, MaskedTextBox phone, MaskedTextBox mobile, TextBox address, MaskedTextBox NIN, DateTimePicker DOB)
        {
            emp[1] = name.Text.Trim();
            emp[2] = surname.Text.Trim();
            emp[3] = phone.Text.Trim();
            emp[4] = mobile.Text.Trim();
            emp[5] = DOB.Value.Date;
            emp[6] = male.Checked ? 1 : 0;
            emp[7] = address.Text.Trim();
            emp[8] = NIN.Text.Trim();

            emp[16] = true;
        }

        #endregion

        KeyValuePair<FormValidationResult, string> DoesValidate()
        {
            return DoesValidate(
                tbFirstName,
                tbLastName,
                rbMale,
                rbFemale,
                tbPhoneNumber,
                tbMobilePhone,
                tbAddress,
                tbNIN,
                dpBirthDate);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var validated = DoesValidate();
            // validation fails
            if (validated.Key == FormValidationResult.Error)
            {
                var message = "Important data is missing:\n" + validated.Value;
                message += "Please fill up the form correctly and try again.";
                MessageBox.Show(
                    message,
                    Resources.MESSAGEBOX_ERROR_FormValidationResultERROR_CAPTION,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return; // fail
            }

            // validation succeeds if user accepts the warning
            if (validated.Key == FormValidationResult.Error)
            {
                var message = "Data incomplete or doubtful:\n" + validated.Value;
                message += "Do you want to add the employee anyway?";

                var decision = MessageBox.Show(
                    message,
                    Resources.MESSAGEBOX_ERROR_FormValidationResultERROR_CAPTION,
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question);
                if (decision == DialogResult.Cancel)
                {
                    return; // user didn't accept; fail
                }
            }

            FillRow(
                Employee,
                tbFirstName,
                tbLastName,
                rbMale,
                tbPhoneNumber,
                tbMobilePhone,
                tbAddress,
                tbNIN,
                dpBirthDate);
            DialogResult = DialogResult.OK;
            
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (Control c in this.tblPersonalDetails.Controls)
            {
                if (c.GetType() == typeof (TextBox) ||
                    c.GetType() == typeof (MaskedTextBox))
                {
                    c.Text = String.Empty;
                }
            }
            rbMale.Checked = true;
            dpBirthDate.Value = DateTime.Now.Date.AddYears(-20);
        }
        
    }
}
